/*
    Matali Physics Demo
    Copyright (c) 2011 KOMIRES Sp. z o. o.
 */
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Komires.MataliPhysics;

namespace MataliPhysicsDemo
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class DefaultShapes
    {
        Demo demo;
        PhysicsScene scene;

        public DefaultShapes(Demo demo)
        {
            this.demo = demo;
        }

        public void Initialize(PhysicsScene scene)
        {
            this.scene = scene;
        }

        public static void CreateShapes(Demo demo, PhysicsScene scene)
        {
            TriangleMesh triangleMesh = null;
            ShapePrimitive shapePrimitive = null;
            Shape shape = null;

            triangleMesh = scene.Factory.TriangleMeshManager.Create("TorusMesh1");
            triangleMesh.CreateTorusY(10, 15, 3.0f, 1.0f);
            if (!demo.Meshes.ContainsKey("TorusMesh1"))
                demo.Meshes.Add("TorusMesh1", new DemoMesh(demo, triangleMesh, demo.Textures["Default"], Vector2.One, true, true, true, false, true, RasterizerState.CullClockwise, false));

            scene.Factory.CreatePhysicsObjectsFromConcave("ConcaveTorus1", triangleMesh);
            scene.Factory.CreatePhysicsObjectsFromConcave("ConcaveTorus2", triangleMesh);

            shapePrimitive = scene.Factory.ShapePrimitiveManager.Create("ConvexTorus1");
            shapePrimitive.CreateConvex(triangleMesh);

            shape = scene.Factory.ShapeManager.Create("ConvexTorus1");
            shape.Set(shapePrimitive, Matrix.Identity, 0.0f);

            triangleMesh = scene.Factory.TriangleMeshManager.Create("UserMesh1");

            TriangleMeshRegion r01 = triangleMesh.TriangleMeshRegionManager.Create("r01");

            Vertex v1 = r01.VertexManager.Create("v1");
            v1.SetPosition(-1.0f, -1.0f, -1.0f);
            Vertex v2 = r01.VertexManager.Create("v2");
            v2.SetPosition(0.0f, -1.0f, 1.0f);
            Vertex v3 = r01.VertexManager.Create("v3");
            v3.SetPosition(1.0f, -1.0f, -1.0f);
            Vertex v4 = r01.VertexManager.Create("v4");
            v4.SetPosition(0.0f, 1.0f, 0.0f);

            Triangle t01 = r01.TriangleManager.Create("t01");
            t01.Index1 = 0;
            t01.Index2 = 1;
            t01.Index3 = 3;
            Triangle t02 = r01.TriangleManager.Create("t02");
            t02.Index1 = 0;
            t02.Index2 = 3;
            t02.Index3 = 2;
            Triangle t03 = r01.TriangleManager.Create("t03");
            t03.Index1 = 2;
            t03.Index2 = 3;
            t03.Index3 = 1;
            Triangle t04 = r01.TriangleManager.Create("t04");
            t04.Index1 = 0;
            t04.Index2 = 2;
            t04.Index3 = 1;

            triangleMesh.Update(true, true);

            if (!demo.Meshes.ContainsKey("UserMesh1"))
                demo.Meshes.Add("UserMesh1", new DemoMesh(demo, triangleMesh, demo.Textures["Default"], Vector2.One, false, false, true, false, false, RasterizerState.CullClockwise, false));

            shapePrimitive = scene.Factory.ShapePrimitiveManager.Create("UserMesh1");
            shapePrimitive.CreateConvex(triangleMesh);

            shape = scene.Factory.ShapeManager.Create("UserMesh1");
            shape.Set(shapePrimitive, Matrix.Identity, 0.0f);

            shapePrimitive = scene.Factory.ShapePrimitiveManager.Create("Cylinder2RY");
            shapePrimitive.CreateCylinder2RY(2.0f, 2.0f, 1.0f);

            shape = scene.Factory.ShapeManager.Create("Cylinder2RY");
            shape.Set(shapePrimitive, Matrix.Identity, 0.0f);

            triangleMesh = scene.Factory.TriangleMeshManager.Create("Cylinder2RY");
            triangleMesh.CreateCylinder2RY(1, 15, 2.0f, 2.0f, 1.0f);
            if (!demo.Meshes.ContainsKey("Cylinder2RY"))
                demo.Meshes.Add("Cylinder2RY", new DemoMesh(demo, triangleMesh, demo.Textures["Default"], Vector2.One, true, true, true, false, true, RasterizerState.CullClockwise, false));

            triangleMesh = scene.Factory.TriangleMeshManager.Create("TubeMesh1");
            triangleMesh.CreateTubeY(1, 15, 2.0f, 1.0f, 0.5f, 1.0f, 0.5f);
            if (!demo.Meshes.ContainsKey("TubeMesh1"))
                demo.Meshes.Add("TubeMesh1", new DemoMesh(demo, triangleMesh, demo.Textures["Default"], Vector2.One, true, true, true, false, true, RasterizerState.CullClockwise, false));
        }

        public void Create()
        {

            Shape cylinderY = scene.Factory.ShapeManager.Find("CylinderY");

            PhysicsObject objectBase = null;   

            objectBase = scene.Factory.PhysicsObjectManager.Create("Cylinder 1");
            objectBase.Shape = cylinderY;
            objectBase.UserTagStr = "CylinderY";
            objectBase.InitLocalTransform.SetPosition(0.0f, 20.0f, 20.0f);
            objectBase.InitLocalTransform.SetScale(2.0f);
            objectBase.Integral.SetDensity(1.0f);

            scene.UpdateFromInitLocalTransform(objectBase);

            objectBase = scene.Factory.PhysicsObjectManager.Create("Cylinder 2");
            objectBase.Shape = cylinderY;
            objectBase.UserTagStr = "CylinderY";
            objectBase.InitLocalTransform.SetPosition(0.0f, 20.0f, 10.0f);
            objectBase.InitLocalTransform.SetScale(1.0f, 2.0f, 2.0f);
            objectBase.Integral.SetDensity(1.0f);

            scene.UpdateFromInitLocalTransform(objectBase);
           
        }
    }
}
